package com.example.jvm.jvm06.lesson45;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Date 2021/7/15
 * @Created by Jonathan
 */
@Slf4j
public class Test {
    private static Test obj = null;

    public static void main(String[] args) throws InterruptedException {
        obj = new Test();
        obj = null;
        // 这两句保证垃圾进行回收标记，finalize能进行自我挽救
        System.gc();
        // finalize的优先级低
        TimeUnit.SECONDS.sleep(3);
        if (obj == null) {
            log.info("obj 已经死亡1。");
        } else {
            log.info("obj成功挽救自己呢1");
        }

        obj = null;
        // 这两句保证垃圾进行回收标记，finalize能进行自我挽救
        System.gc();
        // finalize的优先级低
        TimeUnit.SECONDS.sleep(1);
        if (obj == null) {
            log.info("obj 已经死亡2。");
        } else {
            log.info("obj成功挽救自己呢2");
        }

    }

    @Override
    protected void finalize() throws Throwable {
        log.info("finalize=======复活 哈哈哈======");
        obj = this;
        super.finalize();
    }
}
